Imports OpenArchive.Document.Components
Namespace OpenArchive
    Namespace Document
        Public Class Filedocument
            Inherits Document

            Public Sub New()
                MyBase.New()
            End Sub
            Public Sub New(ByVal File As Byte())
                MyBase.new()
                Dim _filecomp As FileComponent = New FileComponent(File)
                _filecomp.Content = File
                MyBase.Add(_filecomp)
            End Sub
            Public Shadows Sub Add(ByVal FileComp As FileComponent)
                ' ToDo: Check if already exists
                MyBase.Add(FileComp)
            End Sub
            Public Shadows Sub Add(ByVal data As Byte())
                Dim _filecomp As FileComponent = Filecomponent()
                If Filecomponent Is Nothing Then
                    _filecomp = New FileComponent(data)
                    MyBase.Add(_filecomp)
                Else
                    Dim _datastored() As Byte = _filecomp.Content
                    If _datastored Is Nothing Then
                        _filecomp.Content = data
                    Else
                        Dim _newdata(_filecomp.Content.Length + data.Length) As Byte
                        Global.System.Buffer.BlockCopy(_datastored, 0, _newdata, 0, _datastored.Length)
                        Global.System.Buffer.BlockCopy(data, 0, _newdata, _datastored.Length - 1, _newdata.Length)
                        _filecomp.Content = _newdata
                    End If

                End If
            End Sub
            Public ReadOnly Property Content() As Byte()
                Get
                    Return Filecomponent.DataObjects.Content
                End Get
            End Property

            Public Property Filename() As String
                Get
                    Return Filecomponent.Filename
                End Get
                Set(ByVal value As String)
                    Filecomponent.Filename = value
                End Set
            End Property
            Public Property Mimetype() As String
                Get
                    Return Filecomponent.Mimetype
                End Get
                Set(ByVal value As String)
                    Filecomponent.Mimetype = value
                End Set
            End Property
            Public Property Created() As Date
                Get
                    Return Filecomponent.Created
                End Get
                Set(ByVal value As Date)
                    Filecomponent.Created = value
                End Set
            End Property
            Public Property Modified() As Date
                Get
                    Return Filecomponent.Modified
                End Get
                Set(ByVal value As Date)
                    Filecomponent.Modified = value
                End Set
            End Property
            Private ReadOnly Property Filecomponent() As FileComponent
                Get
                    For Each _component As Component In Me.Components
                        ' ToDo Find Infocomponent
                        If TypeOf _component Is FileComponent Then
                            Return DirectCast(_component, FileComponent)
                        End If
                    Next
                    Return Nothing
                End Get
            End Property
        End Class
    End Namespace

End Namespace